<!-- 判断是否看广告，如果今天没有来过就看广告 -->

<template>
	<view class="main">
		<guide v-if="guidePages" :advData="advData" :time="advData.time"></guide>
	</view>
</template>

<script>
	import guide from '@/components/guide/index.vue';
	import Cache from '@/utils/cache';
	import {
		getOpenAdv
	} from '@/api/api.js';
	export default {
		components: {
			guide
		},
		data() {
			return {
				guidePages: false,
				advData: [],
				indexUrl: '/pages/base/index/index'
			};
		},
		onLoad(options) {
			console.log('进入导航页', this.indexUrl)
			if (options.spid) {
				this.indexUrl = this.indexUrl + '?spid=' + options.spid;
				console.log('当前路由', this.indexUrl)
			}
		},
		onShow() {
			this.loadExecution();
		},
		methods: {
			loadExecution() {
				const tagDate = uni.getStorageSync('guideDate') || '',
					nowDate = new Date().toLocaleDateString();
				//判断上次浏览日期，如果等于今天的日期，说明今天已经展示过引导页了
				if (tagDate === nowDate) {
					// 改成 navigateTo 只能跳转到非 tabBar 页面。
					console.log('跳转首页', this.url)
					uni.navigateTo({
						url: this.indexUrl
					});
					return;
				}
				//获取广告，展示广告
				getOpenAdv()
					.then((res) => {
						console.log('看广告！')
						if (res.data.status == 0 || res.data.value.length == 0) {
							console.log('跳转首页', this.indexUrl)
							uni.navigateTo({
								url: this.indexUrl
							});
						} else if (res.data.status && (res.data.value.length || res.data.video_link)) {
							this.advData = res.data;
							uni.setStorageSync('guideDate', new Date().toLocaleDateString());
							this.guidePages = true;
						}
					})
					.catch((err) => {
						uni.navigateTo({
							url: this.indexUrl
						});
					});
			}
		},
		onHide() {
			this.guidePages = false;
		}
	};
</script>

<style>
	page,
	.main {
		width: 100%;
		height: 100%;
	}
</style>